#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include "head.h"

#define err_log(err) do{perror(err);exit(-1);}while(1)
	

//用户注册
void do_register(Msg *msg)
{
	printf("Enter Usrname >\n");
	scanf("%s",msg->user_info.name);
	printf("Enter Password >\n");
	scanf("%s",msg->user_info.passwd);
	msg->cmd = 1;      //命令码为1，注册
	 
	return;
}

//登陆
void do_login(Msg *msg)
{
	printf("Enter Usrname >\n");
	scanf("%s",msg->user_info.name);
	printf("Enter Password >\n");
	scanf("%s",msg->user_info.passwd);
	msg->cmd = 2;      //命令码为2，登陆
	 
	return;
}

//普通用户名查询信息
void usr_do_query(Msg *msg, int sockfd)
{
	char info[128];
	
	msg->cmd = 4;    //按用户名查询，命令码4
	send(sockfd, msg, sizeof(Msg), 0);
	recv(sockfd, msg, sizeof(Msg), 0);
	
	//打印回传信息
	sprintf(info,"name:%s id:%d age:%d year:%d sex:%c salary:%d department:%s", msg->user_info.name, \
			msg->user_info.id, msg->user_info.age, msg->user_info.year, msg->user_info.sex, \
			msg->user_info.salary, msg->user_info.department);

	printf("%s\n",info);
	return;
}

//用户改密码
void usr_do_change(Msg *msg, int sockfd)
{
	msg->cmd = 5;    //用户改密码，命令码5
	printf("enter new password >");
	scanf("%s", msg->user_info.passwd);
	
	send(sockfd, msg, sizeof(Msg), 0);
	recv(sockfd, msg, sizeof(Msg), 0);
	
	//修改密码是否成功的反馈
	printf("change password: %s\n",msg->buf);
	
	return;
}

void usr_do_history(Msg *msg, int sockfd)
{
	char histroyInfo[128];
	
	while(1){
		msg->cmd = 7;    //用户查询历史记录，命令码7
		send(sockfd, msg, sizeof(Msg), 0);
		recv(sockfd, msg, sizeof(Msg), 0);
	
		if(!strncmp(msg->buf, "over", strlen("over"))){
			break;
		}
	
		sprintf(histroyInfo, "%s %s %s", msg->history_info.name,\
				msg->history_info.action, msg->history_info.date);
		printf("%s\n",histroyInfo);		
	}

	return;	
}

//用户菜单
void usr_menu(Msg *msg,int sockfd)
{
	int cmd;

	while(1){
		printf("**********************************************************\n");
		printf("1.query information 2.change password 3.histroy log 4.back\n");
		printf("**********************************************************\n");

		printf("please choose >");
		scanf("%d",&cmd);

		switch(cmd)
	{
		case 1:
			printf("begin query...\n");
			usr_do_query(msg,sockfd);
			break;
		case 2:
			usr_do_change(msg,sockfd);
			break;
		case 3:
			usr_do_history(msg,sockfd);
			break;
		case 4:
			return;
		default:
			break;
		}
	}

	return;
}

//管理员添加用户
void add_user(Msg *msg, int sockfd)
{
	char c;
	
	msg->cmd = 8;    //管理员添加用户信息，命令码8
	
	printf("input name >");
	scanf("%s",msg->user_info.name);

	printf("input id >");
	scanf("%d",&msg->user_info.id);
	
	printf("input age >");
	scanf("%d",&msg->user_info.age);
	
	printf("input year >");
	scanf("%d",&msg->user_info.year);
	getchar();
	
	printf("input sex >");
	scanf("%c",&msg->user_info.sex);
	
	printf("input salary >");
	scanf("%d",&msg->user_info.salary);
	
	printf("input department >");
	scanf("%s",msg->user_info.department);
	getchar();
	
	//设定用户级别是否为管理员
	printf("Is he/she a manager(y/n)?\n");
	scanf("%c",&c);
	if(c == 'y'){
		msg->user_info.managedr = 1;
	}else if(c == 'n'){
		msg->user_info.managedr = 0;
	}
	
	//设置默认密码：000000
	strcpy(msg->user_info.passwd,  "000000");
	
	send(sockfd, msg, sizeof(Msg), 0);
	recv(sockfd, msg, sizeof(Msg), 0);
	//打印反馈
	printf("%s\n", msg->buf);
	
	return;
}

//管理员删除用户
void delete_user(Msg *msg, int sockfd)
{
	msg->cmd = 10;  //删除用户，命令码10
	
	printf("input the user name you want to delete >");
	scanf("%s",msg->user_info.name);
	send(sockfd, msg, sizeof(Msg), 0);
	recv(sockfd, msg, sizeof(Msg), 0);
	
	printf("%s\n",msg->buf);
	return;
}

//管理员修改用户信息
void modify_info(Msg *msg, int sockfd)
{
	int choice;
	char c;
	printf("user name >");
	msg->cmd = 9;     
	scanf("%s",msg->user_info.name);

	printf("*********************************************************************\n");
	printf("1.id 2.age 3.year 4.sex 5.salary 6.department 7. password 8.IsManager\n");
	printf("*********************************************************************\n");
	printf("input choice >");
	scanf("%d",&choice);
	switch(choice)
	{
		case 1:
			printf("input id >");
			scanf("%d",&msg->user_info.id);
			break;
		case 2:
			printf("input age >");
			scanf("%d",&msg->user_info.age);
			break;
		case 3:
			printf("input year >");
			scanf("%d",&msg->user_info.year);
			break;
		case 4:
			printf("input sex >");
			scanf("%c",&msg->user_info.sex);
			break;
		case 5:
			printf("input salary >");
			scanf("%d",&msg->user_info.salary);
			break;
		case 6:
			printf("input department >");
			scanf("%s",msg->user_info.department);
			break;
		case 7:
			printf("input password >");
			scanf("%s",msg->user_info.passwd);
			break;
		case 8:
			printf("Is he/she a manager(y/n)?\n");
			scanf("%c",&c);
			if(c == 'y'){
				msg->user_info.managedr = 1;
			}else if(c == 'n'){
				msg->user_info.managedr = 0;
			}
			break;
		default:
			break;
	}
	
	send(sockfd, msg, sizeof(Msg), 0);
	recv(sockfd, msg, sizeof(Msg), 0);
	
	printf("modify :%s\n",msg->buf);
	
	return;
}

//管理员查询用户信息
void query_user_info(Msg *msg, int sockfd)
{
	char choice[10];
	char info[128];
	printf("query information of all or query by name ?(all/name)");
	scanf("%s",choice);
	
	//查询所有或者通过用户名查询
	if(!strcmp(choice,"all")){
		while(1){
			msg->cmd = 11;    //查询所有用户信息，命令码11
			send(sockfd, msg, sizeof(Msg),0);
			recv(sockfd, msg, sizeof(Msg),0);
				//判断结束条件
			if(!strncmp(msg->buf,"over", strlen("over"))){
				break;
			}

			//打印回传信息
			sprintf(info,"name:%s id:%d age:%d year:%d sex:%c salary:%d department:%s", msg->user_info.name, \
			msg->user_info.id, msg->user_info.age, msg->user_info.year, msg->user_info.sex, \
			msg->user_info.salary, msg->user_info.department);

			printf("%s\n",info);
		}			
	}else if(!strcmp(choice, "name")){
		printf("input name >");
		scanf("%s",msg->user_info.name);
		msg->cmd = 12;                     //按用户名查询，命令码12
		send(sockfd, msg, sizeof(Msg), 0);
		recv(sockfd, msg, sizeof(Msg), 0);
		
		if(!strncmp(msg->buf, "success", strlen("success"))){
					
			//若查找到用户，打印回传信息
			sprintf(info,"name:%s id:%d age:%d year:%d sex:%c salary:%d department:%s", msg->user_info.name, \
			msg->user_info.id, msg->user_info.age, msg->user_info.year, msg->user_info.sex, \
			msg->user_info.salary, msg->user_info.department);
			printf("%s\n",info);
		}else{
			printf("no such user :%s\n",msg->user_info.name);
		}

		
	}else{
		printf("invalid input!\n");
	}
	
	return;
}

//管理员查看历史记录
void query_history_info(Msg *msg, int sockfd)
{
	char choice[10];
	char histroyInfo[128];
	int count = 0;
	
	printf("query histroy action of all or query histroy by name ?(all/name)");
	scanf("%s",choice);
	
	if(!strcmp(choice,"all")){
		while(1){
			msg->cmd = 13;    //查询所有用户历史记录，命令码13
			send(sockfd, msg, sizeof(Msg),0);
			recv(sockfd, msg, sizeof(Msg),0);
			count++;

			if(!strncmp(msg->buf,"over", strlen("over"))){
				break;
			}


			//打印回传信息

			sprintf(histroyInfo, "%s %s %s", msg->history_info.name, msg->history_info.action, msg->history_info.date);
			printf("%s\n",histroyInfo);	

		}			
	}else if(!strcmp(choice, "name")){
		printf("input name >");
		scanf("%s",msg->user_info.name);

		while(1){
			msg->cmd = 7;    //管理员查询历史记录，命令码7
			send(sockfd, msg, sizeof(Msg), 0);
			recv(sockfd, msg, sizeof(Msg), 0);

			if(!strncmp(msg->buf, "over", strlen("over"))){
				break;
			}

			//若查找到用户，打印回传信息
			sprintf(histroyInfo, "%s %s %s", msg->history_info.name, msg->history_info.action, msg->history_info.date);
			printf("%s\n",histroyInfo);
		}

	}else{
		printf("invalid input!\n");
	}
	

	printf("count = %d\n",count);
	return;
	
}


//管理员菜单
void admin_menu(Msg *msg,int sockfd)
{
	int cmd;
	//printf("Welome to staff information management system!\n");
	
	//1.添加用户      2.删除用户  3.修改用户信息  
	//4.查询用户信息  5.查询历史  6.退出	
	while(1){
		printf("******************************************************************************\n");
		printf("1.add user 2.delete user 3.modify user info 4.query user info 5.histroy 6.back\n");
		printf("******************************************************************************\n");

		printf("please choose >");
		scanf("%d",&cmd);

		switch(cmd)
		{
		case 1:
			add_user(msg,sockfd);
			break;
		case 2:
			delete_user(msg,sockfd);
		case 3:
			modify_info(msg,sockfd);
			break;
		case 4:
			query_user_info(msg,sockfd);
			break;
		case 5:
			query_history_info(msg,sockfd);
			break;
		case 6:
			return;
		default:
			break;
		}
	}

	return;
}

int main(int argc ,char *argv[])
{
	if(argc < 2){
		printf("Usage : %s port\n",argv[0]);
		return -1;
	}
	
	int ret;
	int sockfd,confd;
	socklen_t len;
	struct sockaddr_in client,server;
	len = sizeof(client);

	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(sockfd < 0){
		err_log("fail to socket");
	}

	server.sin_family = AF_INET;
	server.sin_addr.s_addr = inet_addr(IP); //服务器IP
	server.sin_port = htons(atoi(argv[1]));     		 //服务器port
	if(connect(sockfd,(struct sockaddr*)&server, len) < 0){
		err_log("fail to connect");
	}

	int cmd;
	Msg msg;
	/*一级菜单*/
	while(1){

		printf("*************************\n");
		printf("1.regsiter 2.login 3.quit\n");
		printf("*************************\n");

		printf("please choose the operation> ");
		scanf("%d",&cmd);

		switch(cmd)
		{
		case 1:
			do_register(&msg);
			send(sockfd,&msg,sizeof(msg),0);
			recv(sockfd,&msg,sizeof(msg),0);
			
			//打印反馈
			printf("register: %s\n",msg.buf);
			
			break;
		case 2:
			do_login(&msg);
			send(sockfd,&msg,sizeof(msg),0);
			recv(sockfd,&msg,sizeof(msg),0);
		
			//for debug
			printf("msg.buf: %s\n",msg.buf);
			
			if(strncmp(msg.buf, "loginok", strlen("loginok")) == 0){
				printf("msg.user_info.managedr:%d\n",msg.user_info.managedr);
				if(msg.user_info.managedr == 1){
					admin_menu(&msg,sockfd);
				}else {
					usr_menu(&msg,sockfd);
				}
			}else if(strncmp(msg.buf, "loginfail", strlen("loginfail")) == 0){
				printf("fail to login!\n");
			}
			
			break;
		case 3:
			printf("exit...\n");
			close(sockfd);
			exit(EXIT_SUCCESS);
		}
	}
		close(confd);

	return 0;
}

